Package org.one.stone.soup.wiki.webcam.multicaster.applet

Source Code of org.one.stone.soup.wiki.webcam.multicaster.applet.WebCamRecorder

package org.one.stone.soup.wiki.webcam.multicaster.applet;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.OutputStream;

import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JOptionPane;

import org.one.stone.soup.grfx.ImageFactory;
import org.one.stone.soup.screen.recorder.ScreenRecorder;
import org.one.stone.soup.screen.recorder.ScreenRecorderListener;

public class WebCamRecorder extends ScreenRecorder {

  private static Player player = null;
  private FrameGrabbingControl fgc;
  private String deviceName;
 
  public WebCamRecorder(OutputStream oStream,ScreenRecorderListener listener,String deviceName)
  {
    super(oStream,listener);
   
    this.deviceName = deviceName;
  }

  @Override
  public Rectangle initialiseScreenCapture() {
    try{
      startPlayer( deviceName );
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return null;
    }
    BufferedImage image = captureScreen(null);
   
    return new Rectangle( image.getWidth(),image.getHeight() );
  }

  @Override
  public BufferedImage captureScreen(Rectangle area) {
        // Grab a frame
        Buffer buf = fgc.grabFrame();
       
        // Convert it to an image
        BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
        Image img = btoi.createImage(buf);
 
        while(img==null)
        {
          buf = fgc.grabFrame();
          btoi = new BufferToImage((VideoFormat)buf.getFormat());
          img = btoi.createImage(buf);
 
          try{Thread.sleep(10);}catch(Exception e){}
        }       
       
      return ImageFactory.createBufferedImage(img);
  }

  public void startPlayer(String deviceName) throws Exception
  {
    System.out.println("Device Name "+deviceName);
    if(deviceName==null || deviceName.length()==0)
    {
      deviceName = "vfw:Microsoft WDM Image Capture (Win32):0";
      System.out.println("Device Name now "+deviceName);
      }
      CaptureDeviceInfo di = CaptureDeviceManager.getDevice( deviceName );
     
      if(di==null)
      {
        deviceName = JOptionPane.showInputDialog("Enter the WebCam device name.\nSee the OpenForum documentation for more information.");
        if(deviceName!=null)
        {
          di = CaptureDeviceManager.getDevice( deviceName );
        }
      }
      if(di==null)
      {
        JOptionPane.showConfirmDialog(null,"No WebCam with the device name could be found.");
        return;
      }
     
      MediaLocator ml = di.getLocator();
 
      player = Manager.createRealizedPlayer(ml);
      player.start();
     
      int state = -1;
      while(state!=Player.Started)
      {
        int newState = player.getState();
        if(newState!=state)
        {
          state = newState;
         
          switch(state)
          {
            case Player.Unrealized:
              System.out.println("Player Unrealized");
              break;
            case Player.Realizing:
              System.out.println("Player Realizing");
              break;
            case Player.Realized:
              System.out.println("Player Realized");
              break;
            case Player.Prefetching:
              System.out.println("Player Prefetching");
              break;
            case Player.Prefetched:
              System.out.println("Player Prefetched");
              break;
            case Player.Started:
              System.out.println("Player Started");
              break;
          }
        }
        try{Thread.sleep(500);}catch(Exception e){}
      }
 
      fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");       
  }

  public void stopPlayer() throws Exception
  {
      player.close();
      player.deallocate();
  }

}
TOP

Related Classes of org.one.stone.soup.wiki.webcam.multicaster.applet.WebCamRecorder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.